<?php

/**
 * LEAGUEMan
 *
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

defined('_VALID_MOS') or die('Direct Access to this location is not allowed.');

switch($task) {

    case 'quickadd':

        $name = mosGetParam($_POST, "name", "");

        $row = LM::getLMTable("Club");
        $row->name = $name;

        if (!$row->check()) {
            LM::redirect("clubs", "Checking Club Failed!");
        }

        if (!$row->store()) {
            LM::redirect("clubs", "Could not store Club!");
        }

        LM::redirect("clubs");

        break;

    case 'bulkadd':

        $template = LM::getTemplate();
        $template->assign("view", "admin/form_clubs_bulkadd.tpl.php");
        $template->assign("project", $project);
        $template->assign("team", $team);
        $template->assign("my", $my);
        $template->display("admin/view.tpl.php");
        break;

    case 'bulksave':

        $clubtext = mosGetParam($_POST, "clubs", "");
        $lines    = explode("\r\n", $clubtext);

        $i = 0;
        foreach ($lines AS $line) {

            $club       = LM::getLMTable("Club");
            $club->name = trim($line);

            if (!$club->store()) {
                $i++;
            }
        }

        LM::redirect("clubs", "Bulksave finished with $i Errors");

        break;

    case 'new':
    case 'edit':

        $row = LM::getLMTable("Club");

        if ($task == "edit") {
            $row->load($cid[0]);
        }

        $template = LM::getTemplate();

        $template->assign("view", "admin/form_clubs.tpl.php");
        $template->assign("project", $project);
        $template->assign("team", $team);
        $template->assign("my", $my);
        $template->assign("row", $row);
        $template->display("admin/view.tpl.php");
        break;

    case 'save':
        $row = LM::getLMTable("Club");
        $row->save($_POST);

        $image = saveImage(
            $mainframe->getCfg("absolute_path") . "/images/stories/leagueman/logos/", "upload", "c{$row->getID()}"
        );

        if ($image) {
            $row->logo = $image;
        }

        if ($row->store()) {
            $msg = "OK";
        } else {
            $msg = "Could not save Club";
        }

        LM::redirect("clubs", $msg);
        break;

    case 'cancel':
        LM::Redirect("clubs");
        break;

    case 'publish':
        $row = LM::getLMTable("Club");
        $row->publish($cid, 1, $my->id);
        LM::redirect("clubs");
        break;

    case 'unpublish';
        $row = LM::getLMTable("Club");
        $row->publish($cid, 0, $my->id);
        LM::redirect("clubs");
        break;

    case 'remove':

        if (!$project->isAdmin()) {
            LM::redirect("clubs", "You are not authorized for this action");
        }

        $total = count($cid);

        $row = LM::getLMTable("Club");

        $i = 0;
        foreach ($cid AS $id) {
            $database->setQuery(
                "SELECT COUNT(team_id)
                   FROM jos_leagueman_team
                  WHERE team_club = " . $database->Quote($id)
            );
            if ($database->loadResult() == 0) {
                if ($row->delete($id)) {
                    $i++;
                }
            }
        }

        LM::redirect("clubs", $i . " from " . $total . " Clubs deleted (due to dependencies or errors)");

        break;


    default:

        $limit      = LM::getSessionValueFromRequest("_limit_", "limit", 50);
        $limitstart = LM::getSessionValueFromRequest("_limitstart_", 'limitstart', 0);
        $search     = LM::getSessionValueFromRequest("_search_{$act}_", 'search', '');
        $search     = $database->getEscaped(trim(strtolower($search)));

        $where = array();
        if ($search) {
            $where[] = "LOWER(name) LIKE '%$search%' OR LOWER(city) LIKE '%$search%'";
        }

        $whereClause = (count($where) ? "\nWHERE " . implode(' AND ', $where) : "");

        $database->setQuery("SELECT COUNT(*) FROM #__leagueman_club" . $whereClause);
        $total = $database->loadResult();
        $pageNav = LM::getPageNav($total, $limitstart, $limit, 1);

        $sql = "SELECT * FROM #__leagueman_club" . $whereClause . "
              ORDER BY name
                 LIMIT $pageNav->limitstart, $pageNav->limit";

        $database->setQuery($sql);
        $rows = $database->loadObjectList();

        $template = LM::getTemplate();

        $template->assign("view", "admin/list_clubs.tpl.php");
        $template->assign("project", $project);
        $template->assign("team", $team);
        $template->assign("rows", $rows);
        $template->assign("search", $search);
        $template->assign("pageNav", $pageNav);
        $template->assign("my", $my);

        $template->display("admin/view.tpl.php");
        break;
}